package mydictionary_;

import java.util.HashMap;
import java.util.Iterator;

/**
 * @author Administrator
 * 工具实现类：集合实现
 */
public class DictionaryDaoMap implements DictionaryDao {
    private HashMap<String,Mydictionary> map=new HashMap<String,Mydictionary>();

    @Override
    public void save(Mydictionary myDictionary) {
        //将传入的参数myDictionary保存集合中
        String key=myDictionary.getEnglish();
map.put(key,myDictionary);
    }

    @Override
    public Mydictionary query(String name) {
        //根据传入的参数name(英文)，得到词典对象
        //判断Map是否包含key,若存在，返回value
    if(map.containsKey(name)){
        return  map.get(name);
    }
        return null;
    }

    @Override
    public void queryAll() {
      //遍历集合
        Iterator iterator=map.keySet().iterator();
        while (iterator.hasNext()){
            String key=(String) iterator.next();
            Mydictionary value=map.get(key);
            System.out.println(value);
        }
    }

    @Override
    public void delete(String name) {
  //要先判断 是否存在，存在则三处，否则提示不存在
        if(map.containsKey(name)){
           map.remove(name);
           System.out.println("该单词已删除");
        }else {
            System.out.println("该单词不存在");
    }
}
}

